package com.toddding.controller;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageInfo;
import com.toddding.domain.Folder;
import com.toddding.service.FolderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


@Controller
@RequestMapping("folder")
public class FolderController {
    @Autowired
    private FolderService folderService;

    /**
     * 文件夹分页查询
     *
     * @param model   数据容器 用于把数据传输到前端页面
     * @param keyWord 搜索的关键字
     * @param page    页码 默认为1
     *                *  @RequestParam(defaultValue = "") 如果前端没有传查询参数 借给一个默认值
     * @return
     */
    @RequestMapping("page.do")
    public String page(Model model, @RequestParam(defaultValue = "") String keyWord, @RequestParam(defaultValue = "1") Integer page) {
        model.addAttribute("page", page);
        //如果关键字 有数据  则放入到容器 便于页面数据回显
        if (StrUtil.isNotBlank(keyWord)) {
            model.addAttribute("keyWord", keyWord);
        }
        PageInfo<Folder> folderPageInfo = this.folderService.queryPage(keyWord, page);
        model.addAttribute("data", folderPageInfo);
        return "/view/folder/list.jsp";
    }

    /**
     * 新增文件夹
     *
     * @param folder
     * @return
     */
    @RequestMapping("add.do")
    public String add(Folder folder) {
        this.folderService.add(folder);
        return "redirect:/folder/page.do"; // 重定向：跳转URL
    }

    /**
     * 删除文件夹
     *
     * @param folderId
     * @return
     */
    @RequestMapping("delete.do")
    public String delete(Integer folderId) {
        this.folderService.delete(folderId);
        return "redirect:/folder/page.do"; // 重定向：跳转URL
    }

    /**
     * 跳转到修改文件夹页面
     *
     * @param folderId
     * @param model
     * @return
     */
    @RequestMapping("toUpdate.do")
    public String toUpdate(Integer folderId, Model model) {
        Folder folder = this.folderService.query(folderId);
        model.addAttribute("folder", folder);
        return "/view/folder/update.jsp";
    }

    /**
     * 具体处理修改文件夹的的请求
     *
     * @param folder
     * @return
     */
    @RequestMapping("update.do")
    public String update(Folder folder) {
        this.folderService.update(folder);
        return "redirect:/folder/page.do"; // 重定向：跳转URL
    }

}
